<template>
  <div class="goods">
    <van-list
      class="goods-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      offset="0"
      @load="onLoad"
    >
      <div
        class="goods-list-item"
        v-for="(item, index) in goods"
        :key="index"
        @click="goDetail('/home/goodsinfo', item.id)"
      >
        <div class="img-box">
          <img v-lazy="item.img_url" alt="" />
        </div>
        <h2 class="title">{{ item.title }}</h2>
        <div class="info">
          <div class="desc">主动降噪</div>
          <p class="price">
            ￥<b>{{ item.sell_price }}元</b>
          </p>
        </div>
      </div>
    </van-list>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loading: false,
      finished: false,
      goods: [],
      page: 0,
      limit: 6,
      total: 0,
    };
  },
  methods: {
    onLoad() {
      setTimeout(async () => {
        await this.getGoods();
        this.loading = false;
        if (this.goods.length >= this.total) {
          this.finished = true;
        }
      }, 1000);
    },

    async getGoods() {
      ++this.page;
      const res = await this.$http.getGoods({
        page: this.page,
        limit: this.limit,
      });
      this.total = res.data.count;
      this.goods = [...this.goods, ...res.data.message];
    },
  },
};
</script>
<style lang="scss">
.goods {
  padding: 10px;
  .goods-list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .goods-list-item {
      border: 1px solid #eee;
      width: 48%;
      margin-bottom: 10px;
      text-align: center;
      .img-box {
        width: 170px;
        height: 170px;
        padding: 10px;
        padding-right: 10px;
        box-sizing: border-box;
        // overflow: hidden;
        img {
          width: 170px;
          height: 170px;
        }
      }
      .title {
        font-size: 14px;
      }
      .info {
        font-size: 12px;
        .desc {
          color: #666;
        }
        .price {
          color: red;
        }
      }
    }
  }
  .van-list__finished-text,
  .van-list__error-text,
  .van-list__loading {
    width: 100%;
    text-align: center;
  }
}
</style>
